<?php
declare (strict_types=1);

namespace app\admin\model\site;

use app\common\logic\Attach;
use app\common\model\BaseModel;

/**
 * Class PersonalData
 *
 * @package app\admin\model\site
 */
class PersonalData extends BaseModel
{
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'site_personal_data';

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 无需创建时间
     *
     * @var boolean
     */
    protected $createTime = false;

    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';

    // 定义全局的查询范围
    protected $globalScope = ['mhm'];

    /**
     * 新增数据事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-14
     *
     * @return   string
     */
    public static function onBeforeInsert($data)
    {
        // 机构
        $data->mhm_id = $data->getAttr('mhm_id') ?: request()->mhm_id;
        // 数据类型
        $data->data_type = $data->getAttr('data_type') ?: 2;
    }

    public function scopeMhm($query)
    {
        $query->where('mhm_id', request()->mhm_id);
    }

    public function iconUrl()
    {
        return $this->belongsTo(Attach::class, 'icon', 'id')
            ->bind(['icon_url' => 'fileurl']);
    }

}
